package org.budo.support.javax.sql.util;

import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.budo.support.assertion.Assert;

/**
 * @author lmw
 */
public class ResultSetUtil {
    public static Object getObject(ResultSet resultSet, String name) {
        try {
            return resultSet.getObject(name);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Boolean hasColumn(ResultSet resultSet, String name) {
        if (null == name || null == resultSet) {
            return null;
        }

        ResultSetMetaData metaData = getMetaData(resultSet);
        int columnCount = getColumnCount(metaData);

        for (int i = 1; i <= columnCount; i++) {
            String columnLabel = getColumnLabel(metaData, i);
            if (columnLabel.equals(name)) {
                return true;
            }
        }

        return false;
    }

    public static String getColumnLabel(ResultSetMetaData metaData, Integer column) {
        try {
            return metaData.getColumnLabel(column);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    private static Integer getColumnCount(ResultSet resultSet) {
        try {
            ResultSetMetaData metaData = resultSet.getMetaData();
            return metaData.getColumnCount();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public static Integer getColumnCount(ResultSetMetaData metaData) {
        try {
            return metaData.getColumnCount();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public static ResultSetMetaData getMetaData(ResultSet resultSet) {
        try {
            return resultSet.getMetaData();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public static Map<String, Object> toMap(ResultSet resultSet) {
        String[] columnLabels = getColumnLabels(resultSet);
        return toMap(resultSet, columnLabels);
    }

    public static String[] getColumnLabels(ResultSet resultSet) {
        if (null == resultSet) {
            return null;
        }

        ResultSetMetaData resultSetMetaData = getMetaData(resultSet);
        Assert.notNull(resultSetMetaData, "resultSet.metaData is null");

        Integer columnCount = getColumnCount(resultSetMetaData);

        String[] columnLabels = new String[columnCount];
        for (int i = 0; i < columnCount; i++) {
            columnLabels[i] = getColumnLabel(resultSetMetaData, i + 1);
        }

        return columnLabels;
    }

    public static Map<String, Object> toMap(ResultSet resultSet, String[] columnLabels) {
        if (null == resultSet || null == columnLabels) {
            return null;
        }

        Map<String, Object> map = new HashMap<String, Object>();
        for (String columnLabel : columnLabels) {
            Object value = getObject(resultSet, columnLabel);
            map.put(columnLabel, value);
        }

        return map;
    }

    public static List<Map<String, Object>> toMapList(ResultSet resultSet, Boolean closeResultSet) {
        if (null == resultSet) {
            return null;
        }

        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        while (next(resultSet)) {
            Map<String, Object> map = toMap(resultSet);
            mapList.add(map);
        }

        if (closeResultSet) {
            close(resultSet);
        }

        return mapList;
    }

    /**
     * 使用后关闭 
     */
    public static List<Map<String, Object>> toMapList(ResultSet resultSet) {
        return toMapList(resultSet, true);
    }

    public static List<List<Object>> table(ResultSet resultSet) {
        if (null == resultSet) {
            return null;
        }

        Integer columnCount = getColumnCount(resultSet);

        List<List<Object>> table = new ArrayList<List<Object>>();
        while (next(resultSet)) {
            List<Object> list = _toList(resultSet, columnCount);
            table.add(list);
        }

        close(resultSet);
        return table;
    }

    /**
     * @see #toMap(ResultSet)
     */
    private static List<Object> _toList(ResultSet resultSet, Integer columnCount) {
        List<Object> list = new ArrayList<Object>();
        for (int i = 0; i < columnCount; i++) {
            Object value = getObject(resultSet, i + 1);
            list.add(value);
        }
        return list;
    }

    public static Object getObject(ResultSet resultSet, Integer rowIndex, Integer columnIndex) {
        if (null == resultSet || null == rowIndex || null == columnIndex || rowIndex < 0 || columnIndex < 0) {
            return null;
        }

        for (int i = 0; i < rowIndex; i++) {
            if (!next(resultSet)) {
                return null;
            }
        }

        Object result = getObject(resultSet, columnIndex);

        close(resultSet);
        return result;
    }

    /**
     * 指定行数，会滚动到指定的行
     * 
     * @param resultSet
     * @param rowIndex    行, 从 0 开始
     * @param columnIndex 列, 从 1 开始
     */
    public static Integer getInteger(ResultSet resultSet, Integer rowIndex, Integer columnIndex) {
        if (null == resultSet || null == rowIndex || null == columnIndex || rowIndex < 0 || columnIndex < 0) {
            return null;
        }

        for (int i = 0; i < rowIndex; i++) {
            if (!next(resultSet)) {
                return null;
            }
        }

        int result = getInt(resultSet, columnIndex);

        close(resultSet);
        return result;
    }

    public static void close(ResultSet resultSet) {
        try {
            resultSet.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 在当前行获取数据
     */
    public static Integer getInt(ResultSet resultSet, Integer columnIndex) {
        try {
            return resultSet.getInt(columnIndex);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static String getString(ResultSet resultSet, Integer columnIndex) {
        try {
            return resultSet.getString(columnIndex);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private static Object getObject(ResultSet resultSet, Integer columnIndex) {
        try {
            return resultSet.getObject(columnIndex);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static Boolean next(ResultSet resultSet) {
        try {
            return resultSet.next();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static InputStream getBinaryStream(ResultSet resultSet, Integer columnIndex) {
        try {
            return resultSet.getBinaryStream(columnIndex);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static Timestamp getTimestamp(ResultSet resultSet, Integer columnIndex) {
        try {
            return resultSet.getTimestamp(columnIndex);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}